package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.ProjectListResp;
import com.xywl.escort.utils.NumberFormatUtils;
import com.xywl.escort.widget.CircleProgress;
import com.xywl.escort.widget.GlideApp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by 32422 on 2018/5/4.
 */

public class ProjectAdapter extends BaseQuickAdapter<ProjectListResp.DataBean.ListBean, BaseViewHolder> {
    public ProjectAdapter(int layoutResId, @Nullable List<ProjectListResp.DataBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProjectListResp.DataBean.ListBean item) {
        ImageView imageView = helper.getView(R.id.img_project);
        GlideApp.with(mContext)
                .load(item.coverUrl)
                .centerCrop()
                .placeholder(R.mipmap.icon_default_picture)
                .into(imageView);

        helper.setText(R.id.text_title, item.name);

        if (null != item.marketPriceYuan) {
            String marketPriceYuan = "市场价/份 " + NumberFormatUtils.getInstance().format(item.marketPriceYuan.doubleValue()) + "元";
            SpannableString spanString = new SpannableString(marketPriceYuan);
            AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(18, true);
            spanString.setSpan(absoluteSizeSpan, marketPriceYuan.lastIndexOf("份") + 1, marketPriceYuan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //再构造一个改变字体颜色的Span
            ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.aid_prompt_color));
            //将这个Span应用于指定范围的字体
            spanString.setSpan(span, marketPriceYuan.lastIndexOf("份") + 1, marketPriceYuan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.text_market_price, spanString);
        }
        if (null != item.subscribePriceYuan) {
            String subscribePriceYuan = "认购价/份 " + NumberFormatUtils.getInstance().format(item.subscribePriceYuan.doubleValue()) + "元";
            SpannableString spanString = new SpannableString(subscribePriceYuan);
            AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(18, true);
            spanString.setSpan(absoluteSizeSpan, subscribePriceYuan.lastIndexOf("份") + 1, subscribePriceYuan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //再构造一个改变字体颜色的Span
            ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.aid_prompt_color));
            //将这个Span应用于指定范围的字体
            spanString.setSpan(span, subscribePriceYuan.lastIndexOf("份") + 1, subscribePriceYuan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.text_price, spanString);
        }

        TextView textFirstLine = helper.getView(R.id.text_first_line);
        TextView textTime = helper.getView(R.id.text_time);
        TextView textSecondLine = helper.getView(R.id.text_second_line);
        CircleProgress circleProgress = helper.getView(R.id.circleProgress);
        circleProgress.setMax(100);
        // 项目状态 1-认购中 2-已售罄 3-派单中 4-待认购 5-成长中 6-分红结束 7-项目失败 -1-未知
        int subscribeRate = (int) item.subscribeRate;
        long available = item.available;
        switch (item.projectStatus) {
            case 1: {
                circleProgress.setProgress(subscribeRate);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.VISIBLE);
                String string = available + "份";
                textFirstLine.setText(string);
                textSecondLine.setText("剩余");
                textTime.setVisibility(View.GONE);
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
                textSecondLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
            }
            break;
            case 2: {
                circleProgress.setProgress(100);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.GONE);
                textFirstLine.setText("已售罄");
                textTime.setVisibility(View.GONE);
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
            }
            break;
            case 3: {
                circleProgress.setProgress(100);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.GONE);
                textFirstLine.setText("派单中");
                textTime.setVisibility(View.GONE);
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
            }
            break;
            case 4: {
                circleProgress.setProgress(0);
                String format = getDateFormat(item.subscribeBeginTime);
                String beginTime = format.concat("开放预定");
                textTime.setText(beginTime);
                textTime.setVisibility(View.VISIBLE);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.VISIBLE);
                String string = available + "份";
                textFirstLine.setText(string);
                textSecondLine.setText("剩余");
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
                textSecondLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
            }
            break;
            case 5: {
                circleProgress.setProgress(100);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.GONE);
                textFirstLine.setText("成长中");
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.normal_text_color));
                textTime.setVisibility(View.GONE);
            }
            break;
            case 6: {
                circleProgress.setProgress(0);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.VISIBLE);
                textFirstLine.setText("分红");
                textSecondLine.setText("结束");
                textTime.setVisibility(View.GONE);
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.main_body_prompt_color));
                textSecondLine.setTextColor(ContextCompat.getColor(mContext, R.color.main_body_prompt_color));
            }
            break;
            case 7: {
                circleProgress.setProgress(0);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.VISIBLE);
                textFirstLine.setText("项目");
                textSecondLine.setText("失败");
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.main_body_prompt_color));
                textSecondLine.setTextColor(ContextCompat.getColor(mContext, R.color.main_body_prompt_color));
                textTime.setVisibility(View.GONE);
            }
            break;
            case -1: {
                circleProgress.setProgress(0);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.GONE);
                textFirstLine.setText("未知");
                textTime.setVisibility(View.GONE);
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.main_body_prompt_color));
            }
            break;
            default: {
                circleProgress.setProgress(0);
                textFirstLine.setVisibility(View.VISIBLE);
                textSecondLine.setVisibility(View.GONE);
                textFirstLine.setText("未知");
                textTime.setVisibility(View.GONE);
                textFirstLine.setTextColor(ContextCompat.getColor(mContext, R.color.main_body_prompt_color));
            }
            break;
        }
    }

    private String getDateFormat(String strData) {
        if (!TextUtils.isEmpty(strData)) {
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());

            Date date;
            try {
                date = simpleDateFormat.parse(strData);
            } catch (ParseException e) {
                e.printStackTrace();
                return strData;
            }

            return simpleDateFormat2.format(date);
        }
        return strData;
    }
}
